package shortestpath.astar.node;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import shortestpath.Node;

abstract class AbstractListedNode<T extends Node> implements ListedNode<T> {

	final T node;

	final ListedNode<T> predecessor;

	AbstractListedNode(T node, ListedNode<T> predecessor) {
		super();
		this.node = node;
		this.predecessor = predecessor;
	}

	@Override
	public ListedNode<T> getPredecessor() {
		return this.predecessor;
	}

	@Override
	public List<T> getWayFromStart() {
		final List<T> result = new LinkedList<T>();

		ListedNode<T> current = this;
		do {
			result.add(current.asNode());
			current = current.getPredecessor();
		} while (current != null);
		Collections.reverse(result);

		return result;
	}

	@Override
	public T asNode() {
		return this.node;
	}

	@Override
	public boolean equals(Object obj) {
		return this.node.equals(obj);
	}

	@Override
	public int hashCode() {
		return this.node.hashCode();
	}

	@Override
	public String toString() {
		return "ListedNode[" + this.node + "]";
	}

}
